package edu.iss.demo.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.exception.StudentNotFound;
import edu.iss.demo.model.Student;
import edu.iss.demo.service.StudentService;
import edu.iss.demo.web.validator.StudentValidator;

@Controller
@RequestMapping(value = "/student")
public class StudentController
{

	@Autowired
	private StudentService studentService;

	@Autowired
	private StudentValidator studentValidator;

	@InitBinder
	private void initBinder(WebDataBinder binder)
	{
		binder.setValidator(studentValidator);
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newStudentPage()
	{
		ModelAndView mav = new ModelAndView("student-new", "student",
				new Student());
		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewStudent(
			@ModelAttribute @Valid Student student, BindingResult result,
			final RedirectAttributes redirectAttributes)
	{

		if (result.hasErrors())
			return new ModelAndView("student-new");

		ModelAndView mav = new ModelAndView();
		String message = "New student " + student.getName()
				+ " was successfully created.";

		studentService.create(student);
		mav.setViewName("redirect:/index.html");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView studentListPage()
	{
		ModelAndView mav = new ModelAndView("student-list");
		List<Student> studentList = studentService.findAll();
		mav.addObject("studentList", studentList);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editStudentPage(@PathVariable Long id)
	{
		ModelAndView mav = new ModelAndView("student-edit");
		Student student = studentService.findById(id);
		mav.addObject("student", student);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editStudent(@ModelAttribute @Valid Student student,
			BindingResult result, @PathVariable Long id,
			final RedirectAttributes redirectAttributes) throws StudentNotFound
	{

		if (result.hasErrors())
			return new ModelAndView("student-edit");

		ModelAndView mav = new ModelAndView("redirect:/index.html");
		String message = "Student was successfully updated.";

		studentService.update(student);

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteStudent(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes) throws StudentNotFound
	{

		ModelAndView mav = new ModelAndView("redirect:/index.html");

		Student student = studentService.delete(id);
		String message = "The student " + student.getName()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
}
//	package com.demo.mvc;
//
//	import java.text.DateFormat;
//	import java.util.Date;
//	import java.util.Locale;
//
//	import org.slf4j.Logger;
//	import org.slf4j.LoggerFactory;
//	import org.springframework.stereotype.Controller;
//	import org.springframework.ui.Model;
//	import org.springframework.ui.ModelMap;
//	import org.springframework.web.bind.annotation.ModelAttribute;
//	import org.springframework.web.bind.annotation.RequestMapping;
//	import org.springframework.web.bind.annotation.RequestMethod;
//
//	import com.demo.mvc.model.Student;
//
//	/**
//	 * Handles requests for the application home page.
//	 */
//	@Controller
//	public class HomeController {
//		
//		private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
//		
//		/**
//		 * Simply selects the home view to render by returning its name.
//		 */
//		@RequestMapping(value = "home", method = RequestMethod.GET)
//		public String home(Locale locale, Model model) {
//			logger.info("Welcome home! The client locale is {}.", locale);
//			
//			Date date = new Date();
//			DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
//			
//			String formattedDate = dateFormat.format(date);
//			
//			model.addAttribute("serverTime", formattedDate );
//			
//			return "home";
//		}
//		
//		@RequestMapping(value = "hello", method = RequestMethod.GET)
//		public String hello(Locale locale, Model model)
//		{
//			return "hello";
//		}
//		
//		@RequestMapping(value = "student", method = RequestMethod.GET)
//		public String student(Locale locale, Model model)
//		{
//			return "student";
//		}
//		
//		@RequestMapping(value = "addStudent", method = RequestMethod.POST, params="save")
//		public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model)
//		{
//			model.addAttribute("name", student.getName());
//			model.addAttribute("age", student.getAge());
//			model.addAttribute("s_id", student.getId());
//			return "studentinfo";
//		}
//		
//		@RequestMapping(value = "addStudent", method = RequestMethod.POST, params="edit")
//		public String editStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model)
//		{
////			model.addAttribute("name", student.getName());
////			model.addAttribute("age", student.getAge());
////			model.addAttribute("s_id", student.getId());
//			return "hello";
//		}
//	}
//
//
//}
